package com.china08.yxyapi.api.manage.sch.office2.inner.send;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.DefaultImageUtil;

@CrossOrigin
@RestController
public class SchOfficeInnerSendListController extends YxyApiBaseController {

    @Autowired
    YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @RequestMapping(value = "/api_yw/manage/sch/office/inner/send/list", method = RequestMethod.POST)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        String schoolId = getProfile(request, response).getSchoolId();
        String userId = getProfile(request, response).getId();
        int status = 0;

        RespModel respModel = new RespModel();
        // 发件人列表
        Page<YxyNoticeInner> page = yxyNoticeInnerRepository.findBySchoolIdAndUserIdAndDeleteStatus(schoolId, userId, status, pageable);

        List<Obj> returnList = new ArrayList<Obj>();

        List<YxyNoticeInner> list = page.getContent();
        for (YxyNoticeInner yxyNoticeInner : list) {
            String receiverUserNick = yxyNoticeInner.getAllReceiverUserNick(",");
            long readManCount = yxyNoticeInner.getReadManCount(1);
            String readManUserNicknameStr = yxyNoticeInner.getReadManUserNicknameStr(0, "、 ");
            String createdDate = DateToolUtils.format(yxyNoticeInner.getCreatedDate(), "yyyy-MM-dd");

            Obj obj = new Obj();
            obj.id = yxyNoticeInner.getId();
            String avatar = yxyNoticeInner.getAvatar();
            avatar = DefaultImageUtil.default1(avatar);
            obj.avatar = avatar;
            obj.title = yxyNoticeInner.getTitle();
            obj.content = yxyNoticeInner.getContent();
            obj.userNick = yxyNoticeInner.getUserNick();
            obj.imgList = yxyNoticeInner.getImage();
            obj.attachmentName = yxyNoticeInner.getAttachmentName();
            obj.attachmentPath = yxyNoticeInner.getAttachmentPath();
            obj.createdDate = createdDate;
            obj.receiverUserNick = receiverUserNick;
            obj.readManCount = readManCount;
            obj.readManUserNicknameStr = readManUserNicknameStr;

            returnList.add(obj);
        }

        respModel.pageNum = page.getNumber() + 1;
        respModel.totalElements = page.getTotalElements();
        respModel.returnList = returnList;
        return respModel;
    }

    public static class Obj {
        public String id;
        public String avatar;
        public String createdDate;
        public String receiverUserNick;
        public long readManCount;
        public String readManUserNicknameStr;
        public String userNick;
        public String title;
        public String content;
        public List<String> imgList;
        public String attachmentPath;
        public String attachmentName;
    }

    public static class ReqModel extends YxyApiPageReqModel {

    }

    public static class RespModel {
        public Integer pageNum = 1;
        public long totalElements = 0;
        public List<Obj> returnList = new ArrayList<Obj>();
    }
}
